/**
  @creator: cilang
  @qq: 1019383856
  @bili: https://space.bilibili.com/433915419
  @gitee: https://gitee.com/OpencvLZG
  @github: https://github.com/OpencvLZG
  @since: 2023/11/26
  @desc: //TODO
**/

package controller

import (
	"ginFlutterBolg/internal/model"
	"ginFlutterBolg/internal/service"
	. "ginFlutterBolg/internal/util"
	"github.com/gin-gonic/gin"
	"strconv"
)

type (
	UserFriendController struct {
	}
)

// FriendUser 关注用户
func (u *UserFriendController) FriendUser(c *gin.Context) {
	userController := new(UserController)
	user, err := userController.GetUserInfo(c)
	if err != nil {
		ResponseStatusOk(c, 400, "数据获取错误", err.Error())
		return
	}
	userFriend := new(model.UserFriend)
	if err := c.Bind(&userFriend); err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}
	userFriend.UserId = user.Id
	userFriendService := new(service.UserFriendService)
	userService := new(service.UserService)
	friendUser, err := userService.GetUserByUid(userFriend.FriendId)
	if err != nil {
		ResponseStatusOk(c, 400, "获取关注对象失败", "")
		return
	}
	userFriend.FriendName = friendUser.Username
	err = userFriendService.FriendUser(userFriend)
	if err != nil {
		ResponseStatusOk(c, 400, "数据库插入失败", "")
		return
	}
	ResponseStatusOk(c, 200, "关注成功", "")

}

// GetUserFriendList 获取用户关注列表
func (u *UserFriendController) GetUserFriendList(c *gin.Context) {
	page := c.Query("page")
	pageInt, err := strconv.Atoi(page)
	if err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}
	userController := new(UserController)
	user, err := userController.GetUserInfo(c)
	if err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}
	if err != nil {
		ResponseStatusOk(c, 400, "数据获取错误", err.Error())
		return
	}
	userFriendService := new(service.UserFriendService)
	userFriendList, err := userFriendService.GetFriendListOffset(pageInt, user.Id)
	if err != nil {
		ResponseStatusOk(c, 400, "数据查询失败", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "获取成功", gin.H{
		"list": userFriendList,
	})
}
